import config from "../config/index";

class Api {

	async getRequest(params) {
		wx.showLoading({
			title: '加载中..'
		});
		return new Promise((resolve, reject) => {
			wx.request({
				method: 'GET',
				url: config.checkBaseUrl() + params.url,
				data: params.data,
				header: Object.assign({
					token: wx.getStorageSync('token')
				}, params.header),
				success: (res) => {
					this.inter(resolve, reject, res.data)
				}
			});
		})
	}

	async postRequest(params) {
		return new Promise((resolve, reject) => {
			wx.request({
				method: 'POST',
				url: config.checkBaseUrl() + params.url,
				data: params.data,
				header: Object.assign({
					token: wx.getStorageSync('token')
				}, params.header),
				success: (res) => {
					this.inter(resolve, reject, res.data)
				}
			});
		})
	}

	inter(resolve, reject, data) {
		wx.hideLoading();
		switch (data.status) {
			case 200:
				resolve(data.result)
				break;

			case 404:
				Toast.fail(data.msg);
				throw new Error(data.msg);
				break;

			default:
				break;
		}
	}
}

export default new Api()
